<?php

namespace App\Console\Commands\Activity;


use App\Library\FeiShu\SendFs;
use App\Models\Activity\ShopOrderActivityModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\Activity\ShopOrderActivityService;
use App\Service\System\SystemNotifyService;
use App\Service\WorkOrder\Appraise\NotifyService;
use Illuminate\Console\Command;
use Kuafu\GeneralFs\BuildFs;

class SendActivityFeedback extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'activity:send-activity-feedback {--activityId=} {--notificationId=} {--username=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '活动反馈通知';

    public function handle()
    {
        $activityId = $this->option('activityId');
        $messageId  = $this->option('notificationId');
        $username   = $this->option('username');

        if (isPre()) {
            $this->info('预发环境不发送');
            return;
        }

        $params = (object)[
            'businessType' => ShopOrderActivityModel::BUSINESS_TYPE_4,
            "id"           => $activityId,
            "export"       => 1
        ];

        $shopChunkList = ShopOrderActivityService::dataStatisticsShopList($params)
                                                 ->pluck('shopId')
                                                 ->chunk(500)
                                                 ->toArray();

        $allUserList = [];
        foreach ($shopChunkList as $shopList) {
            $userList    = RmsUserRemote::getShopManagerUserByShopId($shopList)['lists'];
            $allUserList = array_merge($allUserList, $userList);
        }

        // 未报名的活动人员
        $userIdList = array_values(array_unique(array_column($allUserList, 'userId')));

        SystemNotifyService::preSend($messageId, $userIdList);

        BuildFs::newRobot(SendFs::ROBOT_WARNING)
               ->setTitleContent('推送品牌活动消息',
                   "推送人:{$username}" . PHP_EOL . "活动ID:" . $activityId . PHP_EOL . "消息ID:" . $messageId . PHP_EOL)
               ->setAtUid('2c54915f')
               ->send();
    }
}
